;;; init-theme.el -*- lexical-binding: t; -*-

;;----------------------------------------------------------------------
;; Theme hook
;;----------------------------------------------------------------------

(defvar after-load-theme-hook nil
  "Hook runs after a color theme is loaded using `load-theme'.")

(defvar canary--current-theme (canary/standardize-theme canary\theme))

(define-advice load-theme (:after (theme &rest _))
  "Run `after-load-theme-hook."
  (setq canary--current-theme theme)
  (run-hooks 'after-load-theme-hook))

(when *sys-is-mac-x*
  (add-to-list 'default-frame-alist '(ns-appearance . dark))
  (add-to-list 'default-frame-alist '(ns-transparent-titlebar . t))
  (add-hook 'after-load-theme-hook (lambda ()
                                     (let ((bg (frame-parameter nil 'background-mode)))
                                       (set-frame-parameter nil 'ns-appearance bg)
                                       (setcdr (assq 'ns-appearance default-frame-alist) bg)))))



;;----------------------------------------------------------------------
;; Load theme
;;----------------------------------------------------------------------

(define-advice canary/load-theme (:around (orig-fn theme))
  "Setup config for doom-theme before and after load it."
  (if (canary/is-doom-theme-p theme)
      (progn
        (setq doom-themes-enable-bold t)
        (setq doom-themes-enable-italic t)

        (funcall orig-fn theme)

        ;; Enable flashing mode-line on errors
        (doom-themes-visual-bell-config)
        ;; Corrects (and improves) org-mode's native fontification.
        (doom-themes-org-config)
        ;; Enable custom treemacs theme (all-the-icons must be installed!)
        (doom-themes-treemacs-config))
    (funcall orig-fn theme)))

(add-to-list 'custom-theme-load-path (expand-file-name "tao-theme" canary-themes-dir))
(require 'doom-themes)
(require 'tao-theme)

;; Load theme after init
(add-hook 'after-init-hook (lambda ()
                             (when (or (canary/is-doom-theme-p canary\theme)
                                       (canary/is-tao-theme-p canary\theme))
                               (canary/load-theme canary\theme))))

;; Make certain buffer grossly incandescent
;; (define-advice solaire-mode-swap-bg (:around (orig-fn &rest _))
;;   (when (canary/is-doom-theme-p canary--current-theme)
;;     (funcall orig-fn)))
;; (use-package solaire-mode
;;   :functions persp-load-state-from-file
;;   :hook (((after-change-major-mode after-revert ediff-prepare-buffer) . turn-on-solaire-mode)
;;          (minibuffer-setup . solaire-mode-in-minibuffer)
;;          (after-load-theme . solaire-mode-swap-bg))
;;   :config
;;   (advice-add #'persp-load-state-from-file
;;               :after #'solaire-mode-restore-persp-mode-buffers))

(provide 'init-theme)
